package com.fr.base.core.antlr;

import com.fr.base.core.antlr.collections.AST;

public class ParseTreeRule extends ParseTree
{
  public static final int INVALID_ALT = -1;
  protected String ruleName;
  protected int altNumber;

  public ParseTreeRule(String paramString)
  {
    this(paramString, -1);
  }

  public ParseTreeRule(String paramString, int paramInt)
  {
    this.ruleName = paramString;
    this.altNumber = paramInt;
  }

  public String getRuleName()
  {
    return this.ruleName;
  }

  protected int getLeftmostDerivation(StringBuffer paramStringBuffer, int paramInt)
  {
    int i = 0;
    if (paramInt <= 0)
    {
      paramStringBuffer.append(' ');
      paramStringBuffer.append(toString());
      return i;
    }
    AST localAST = getFirstChild();
    i = 1;
    while (localAST != null)
    {
      if ((i >= paramInt) || (localAST instanceof ParseTreeToken))
      {
        paramStringBuffer.append(' ');
        paramStringBuffer.append(localAST.toString());
      }
      else
      {
        int j = paramInt - i;
        int k = ((ParseTree)localAST).getLeftmostDerivation(paramStringBuffer, j);
        i += k;
      }
      localAST = localAST.getNextSibling();
    }
    return i;
  }

  public String toString()
  {
    if (this.altNumber == -1)
      return '<' + this.ruleName + '>';
    return '<' + this.ruleName + "[" + this.altNumber + "]>";
  }
}